package com.dc.di.sf.dt;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.axiom.om.OMElement;

import com.dc.di.sf.utils.OMUtils;

public class DataSet implements DTObject<DataSet> {

	public static final String TAG_DATASET = "DataSet";

	private DataStructure dataStructure;
	private List<RecordData> recordDatas = new ArrayList<RecordData>();

	public DataSet(DataStructure dataStructure) {
		this.dataStructure = dataStructure;
	}

	/**
	 * @return the dataRecordList
	 */
	public List<RecordData> getRecordDatas() {
		return recordDatas;
	}

	/**
	 * @param dataRecordList
	 *            the dataRecordList to set
	 */
	public void setRecordDatas(List<RecordData> recordDatas) {
		this.recordDatas = recordDatas;
	}

	public OMElement toXML() {
		OMElement dataset = OMUtils.createOMElement(TAG_DATASET);
		for (RecordData dataRecord : recordDatas)
			dataset.addChild(dataRecord.toXML());
		return dataset;
	}

	public DataSet fromXML(OMElement xml) {
		if (xml != null)
			for (Iterator itr = OMUtils.getChildren(xml, RecordData.TAG_DATARECORD); itr.hasNext();) {
				recordDatas.add(new RecordData(dataStructure).fromXML((OMElement) itr.next()));
			}
		return this;
	}

	public void addRecordData(RecordData recordData) {
		recordDatas.add(recordData);
	}

	public void removeRecordData(int index) {
		recordDatas.remove(index);
	}

	/* (non-Javadoc)
	 * @see com.dc.di.sf.dt.DTObject#toXMLString()
	 */
	public String toXMLString() {
		StringBuilder sb = new StringBuilder();
		sb.append(OMUtils.generateOMElementBeginString(TAG_DATASET));
		for (RecordData dataRecord : recordDatas)
			sb.append(dataRecord.toXMLString());
		sb.append(OMUtils.generateOMElementEndString(TAG_DATASET));
		return sb.toString();
	}

}
